﻿using System;
using System.Data;
using msBsc.AppLogic.DataAlc;

namespace msBsc.WebUi
{
    public partial class wfSysGoodsFinder : msBsc.Cls.BPageHt
    {

        //CBizShpCommercialTenantInfo objShpCommercialTenantInfo = new CBizShpCommercialTenantInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                //System.Random rd = new Random();
                //sRadom.Value = this.GetRadomString();
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
                //DataSet ds;
                //using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                //{
                //    ds = oCDbAccess.QueryBySQL("SELECT FLOOR_ID,MAIN_GOODS_NME FROM SHP_GOODS_FLOOR");
                //    ds.Tables[0].TableName = "SHP_GOODS_FLOOR";

                //}
                //BoundComboBox("FLOOR_ID", ds, "MAIN_GOODS_NME", "FLOOR_ID", "", "", true);
            }
        }
        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (this.gtGridRet != null)
            {
                if (this.gtGridRet.action == "load" || this.gtGridRet.action == "export")//处理GRID的数据原获取和数据导出
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    if (ds != null && ds.Tables.Count > 0)
                        SetReturnDatatable(ds.Tables[0], "GOODS_ID");//CONSTANT_CATEGORY_ID为表的KEY字段，可填可不填
                }
                //处理数据保存 
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
            else if (strAction == "btnNotSave")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("VIEW");
            }
            else if (strAction == "btnConfirm")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                doSetSelectSpecail(this.GetParamHtValue("CHECK_ID"),"True");
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "btnCancel")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                doSetSelectSpecail(this.GetParamHtValue("CHECK_ID"),"False");
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "btnDelGoods")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnDelGoods(this.GetParamHtValue("CHECK_ID"));
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "btnDelSpecail")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnDelSpecail(this.GetParamHtValue("CHECK_ID"));
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "dobtnAddSpecail")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnAddSpecail(this.GetParamHtValue("CHECK_ID"), this.GetParamHtValue("dplSpecialSelling"));
                this.ExecJScript("btnDo('mFinder');");
            }

            base.InitialByParam();
        }
        private void doSetSelectSpecail(string strIds,string sSid)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                //System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                //transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                //transactionOption.Timeout = new TimeSpan(0, 10, 0);
                //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                //{
                    DataSet ds = objCBizShpOrderForm.SelectByIds(strIds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["SPECIAL_SELLING_CID"].ToString() !=string.Empty)
                        {
                            ds.Tables[0].Rows[i]["IS_SPECIAL_SELECT"] = sSid; 
                        }
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                //    ts.Complete();
                //    ts.Dispose();
                //}
            }
        }

        private void dobtnDelGoods(string strIds)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    DataSet ds = objCBizShpOrderForm.SelectByIds(strIds);
                    for (int i = ds.Tables[0].Rows.Count-1; i >=0; i--)
                    {
                        ds.Tables[0].Rows[i].Delete();
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                    ts.Complete();
                    ts.Dispose();
                }
            }
        }

        private void dobtnDelSpecail(string strIds)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                //System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                //transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                //transactionOption.Timeout = new TimeSpan(0, 10, 0);
                //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                //{
                    DataSet ds = objCBizShpOrderForm.SelectByIds(strIds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["SPECIAL_SELLING_CID"].ToString() !=string.Empty)
                        {
                            ds.Tables[0].Rows[i]["SPECIAL_SELLING_CID"] = ""; 
                        }
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                //    ts.Complete();
                //    ts.Dispose();
                //}
            }
        }

        private void dobtnAddSpecail(string strIds,string strSpCid)
        {
            if (strSpCid == string.Empty)
            {
                throw new Exception("请先选择特卖类型！"); 
            }
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                //System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                //transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                //transactionOption.Timeout = new TimeSpan(0, 10, 0);
                //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                //{
                    DataSet ds = objCBizShpOrderForm.SelectByIds(strIds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["SPECIAL_SELLING_CID"].ToString() != strSpCid)
                        {
                            ds.Tables[0].Rows[i]["SPECIAL_SELLING_CID"] = strSpCid; 
                        }
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                //    ts.Complete();
                //    ts.Dispose();
                //}
            }
            this.ExecJScript("ClosePop('dvItemEdit1');");
        }

         
        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = strMode; switch (strMode)
            {
                case "EDIT":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", true);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "ADD":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "VIEW":
                    EnabledLinkBtn("btnEdit", true);
                    EnabledLinkBtn("btnSave", false);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnAddSpecail", true);
                    EnabledLinkBtn("btnDelSpecail", true);
                    EnabledLinkBtn("btnConfirm", true);
                    EnabledLinkBtn("btnCancel", true);
                    this.ExecJScript("doCancelGt('myGrid1');");
                    break;
            }
        }
        //获取数据原,"DP_DATA"字符一定不要改变，否则系统不能自动清除缓存
        public DataSet GetDisplayData(string strgtGridID, string strSrcF, string strsRadom)
        {
            if (strgtGridID == "myGrid1")
            {
                DataSet ds;
                if (strSrcF == "mFinder" || !this.SessionIsExists(strsRadom + "DP_DATA", "1"))//点击查询按钮时，或需要数据而没有缓存时，根据条件取出数据记录
                {
                    //string strCOMM_TENANT = this.GetParamHtValue("COMM_TENANT");
                    //string strLEVEL_ONE = this.GetParamHtValue("LEVEL_ONE");
                    //using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                    //{
                    //    ds = oCDbAccess.QueryBySQL("SELECT * FROM SHP_GOODS_TYPE_LEVEL_ONE WHERE LEVEL_ONE_NME LIKE '%" + strLEVEL_ONE + "%' AND  SHOW_PAGE_URL LIKE '%" + strURL_CDE + "%'");
                    //    ds.Tables[0].TableName = "SHP_GOODS_TYPE_LEVEL_ONE";
                    //} 
                    using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpGoodsInfo())
                    {
                        ds = objCBizShpOrderForm.SelectGoodsListForSysFinder(
                              this.GetParamHtValue("COMM_USER_NME")
                            , this.GetParamHtValue("GOODS_NME")
                            , this.GetParamHtValue("SPECIAL_SELLING_CID") );
                        ds.AcceptChanges();
                    }
                    SetSession(strsRadom + "DP_DATA", ds, "1");
                    //查询时，将页面置为查看状态
                    if (strSrcF == "mFinder")
                        SetMode("VIEW");
                }
                else
                {
                    ds = (DataSet)this.GetSession(strsRadom + "DP_DATA", "1");
                }
                return ds;
            }
            else
            {
                return null;
            }
        }

    }
}